package com.googlecode.jmicroerp.wicket.butoaie;

import com.googlecode.jmicroerp.wicket.DefaultViewPage;
import com.googlecode.jmicroerp.wicket.SecuredWebSession;
import com.googlecode.jmicroerp.wicket.components.FileResource;

import com.googlecode.jmicroerp.service.ButoiService;
import com.googlecode.jmicroerp.service.ApometreService;
import com.googlecode.jmicroerp.service.BonService;

import com.googlecode.jmicroerp.wicket.butoaie.ButoaieDetailsPage;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.image.Image;

import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.math.BigDecimal;
import java.util.List;

@SuppressWarnings("serial")
public class ButoaiePage extends DefaultViewPage {

    @SpringBean(name = "butoiService")
    private ButoiService butoiService;
    @SpringBean(name = "apometreService")
    private ApometreService apometreService;
    @SpringBean(name = "uploadPath")
    private String uploadPath;
    @SpringBean(name = "bonService")
    private BonService bonService;
    
    private Date today = new Date(); // data curenta    
    private Date azi = addDays(today, 0); // data azi resetata la miezul noptii
    private Date ieri = addDays(today, -1); // data ieri cu data curenta - o zi

    public static Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.DATE, days);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        //cal.set(Calendar.AM_PM, Calendar.AM);
        cal.set(Calendar.MILLISECOND, 0);
                 
        return cal.getTime();
    }
    private Locale locale = new Locale("ro");
    
    /* final Label lblVandAzi = new Label("vanzariAzi", bonService.queryVanzariAzi()); */
    private class ButoiView extends ListView<Object[]> {

        public ButoiView(String id, List<Object[]> data) {
            super(id, data);
        }
        
        @Override
        protected void populateItem(ListItem<Object[]> li) {
            final Object[] object = li.getModelObject();
            li.add(new Label("denumire", object[0].toString()));
            li.add(new Label("capacitate", object[3].toString()));
            li.add(new Label("cantitate", object[4].toString())); 
            li.add(new Label("pret", object[5].toString())); 
            
            BigDecimal cantVand = bonService.queryCantitateProduseByMagazin((Long) object[8], (Long) object[9]); // dupa idMagazin si idProdus
            li.add(new Label("vandut", cantVand.toString()));
            
            Object[] indexObjI = apometreService.queryByRecipientAll((Long) object[7], ieri);
            // li.add(new Label("indexDataI", indexObjI == null ? "necitit" : DateFormat.getDateInstance(DateFormat.SHORT).format(indexObjI[0])));
            li.add(new Label("indexDataI", DateFormat.getDateInstance(DateFormat.SHORT, locale).format(ieri)));
            li.add(new Label("indexReadI", indexObjI == null ? "0.00" : indexObjI[1].toString()));
            
            Object[] indexObjA = apometreService.queryByRecipientAll((Long) object[7], azi);
            // li.add(new Label("indexDataA", indexObjA == null ? "necitit" : DateFormat.getDateInstance(DateFormat.SHORT).format(indexObjA[0])));
            li.add(new Label("indexDataA", DateFormat.getDateInstance(DateFormat.SHORT, locale).format(azi)));
            li.add(new Label("indexReadA", indexObjA == null ? "0.00" : indexObjA[1].toString()));
            
            BigDecimal diferentaIdx = BigDecimal.ZERO;
            BigDecimal diferentaVal = BigDecimal.ZERO;
            if (indexObjI == null)  {
            	diferentaIdx = BigDecimal.ZERO;
            	diferentaVal = cantVand;
            } else if (indexObjA == null) {
            	diferentaIdx = BigDecimal.ZERO;
            	diferentaVal = cantVand;
            } else {
                BigDecimal valI = new BigDecimal(indexObjI[1].toString());
                BigDecimal valA = new BigDecimal(indexObjA[1].toString());
                diferentaIdx = valA.subtract(valI);
                diferentaVal = diferentaIdx.subtract(cantVand);
            }
            li.add(new Label("lblDifIdx", diferentaIdx.toString()));
            li.add(new Label("lblDifVal", diferentaVal.toString()));
            
            String caleImagine = object[6].toString();
            Image img = new Image("caleImagine", new FileResource(uploadPath, caleImagine)) {
                @Override
                protected boolean shouldAddAntiCacheParameter() {
                    return true;
                }
            };
            li.add(img);

            Link indexLink = new Link("indexLink") {
                @Override
                public void onClick() {
                    setResponsePage(new IndexAddPage((Long) object[7]));
                }
            };
            li.add(indexLink);
            
            Link detailLink = new Link("detailLink") {
                @Override
                public void onClick() {
                    setResponsePage(new ButoaieDetailsPage((Long) object[7]));
                }
            };
            li.add(detailLink);
            
        }
    }

    public ButoaiePage(final PageParameters parameters) {
        /* super(parameters); */
        String result = ""; 
        add(new FeedbackPanel("feedback"));
/* 		if (parameters.containsKey("msg")) { 
			result = parameters.getString("msg");
		} */
        /* info(result); */
        Date now = new Date();
        DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        
        Label labelDateTime = new Label("currentTime", shortDf.format(now));
        this.add(labelDateTime);
        add(new Label("helloLabel", "Vizualizare Butoaie"));
        
        final ButoiView butoiView = new ButoiView("butoiList", butoiService.queryButoaieAll(((SecuredWebSession) getSession()).getMagazin().getId()));
        add(butoiView);
    }
}


